#include "aduc812.h"
#include "max.h"
#include "interrupt.h"
#include "led.h"
#include "rs232.h"
#include "keyboard.h"

char keyboard_scan() {
	char i, tmp;
	for (i = 0; i < 4; i++) {
		write_max(KB, ~(0x01 << i));
		tmp = ((~read_max(KB)) >> 4) & 0x0F;
		if (tmp == 0) continue;
		if (tmp == 1) return i*4 + 0;
		if (tmp == 2) return i*4 + 1;
		if (tmp == 4) return i*4 + 2;
		if (tmp == 8) return i*4 + 3;
	}
	return -1;
}

char map(char key) {
	char keys[16] = {'1', '4', '7', '*', '2', '5', '8', '0', '3', '6', '9', '#', 'A', 'B', 'C', 'D'};
	return key < 16 ? keys[key] : '!';
}
